package StructuralPattern.Bridge;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class XMLUtil {
    public static Object getBean(String args) {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse("src\\main\\resources\\config.xml");
            NodeList nodeList = null;
            Node classNode = null;
            String name = null;
            if (args.equals("image")) {
                nodeList = document.getElementsByTagName("BridgeImage");

            } else if (args.equals("os")) {
                nodeList = document.getElementsByTagName("BridgeSystem");
            } else {
                return null;
            }
            classNode = nodeList.item(0).getFirstChild();
            name = classNode.getNodeValue();

            Class<?> aClass = Class.forName(name);
            Object o = aClass.newInstance();
            return o;
        } catch (Exception e ) {
            e.printStackTrace();
            return null;
        }


    }
}
